<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" draggable  :width="1200"  destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
            <el-form-item label="标题" prop="name">
                <el-input v-model="form.name" clearable></el-input>
        	</el-form-item>

            <el-form-item label="文章分类" prop="artclecate_id">
                <el-select v-model="form.artclecate_id" clearable placeholder="请选择文章分类">
                    <el-option v-for="v in artlecate" :key="v.id" :label="v.name" :value="v.id"></el-option>
                </el-select>
        	</el-form-item>


            <el-card shadow="never" header="文章内容" style="margin-top: 5px;">
                <sc-editor v-model="form.content" placeholder="请输入内容" :height="400"></sc-editor>
            </el-card>



            <el-form-item label="显示" prop="switch">
                <el-switch v-model="form.switch" :active-value="1" :inactive-value="0"></el-switch>
        	</el-form-item>



        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='read'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    import { defineAsyncComponent } from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));

    export default {
        emits: ['success', 'closed'],
        components: {
            scEditor
        },
        data() {
            return {
                mode: "create",
                titleMap: {
                    read: '查看',
                    create: '新增',
                    update: '编辑',
                },
                visible: false,
                isSaveing: false,
                form: {
                    id: 0,
					name: '',
					content: '',
					artclecate_id: '',
					switch: '',
                },
                rules: {
                },
                artlecate:[],
            }
        },
        mounted() {
			this.getArtlecate();
		},
        methods: {
            open(mode='create'){
                this.mode = mode;
                this.visible = true;
                return this
            },
            async getArtlecate(){
				var res = await this.$API.artclecate.read.get();
				this.artlecate = res.data.list;
			},
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = await this.$API.artcle[this.mode].post(this.form);
                        this.isSaveing = false;
                        if(res.code == 200){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success(res.message)
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }else{
                        return false;
                    }
                })
            },
            setData(data){
                this.$TABLE.assign(this,data);
            }
        }
    }
</script>